package com.leigang.java.code.batch.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @program: yaner-boot
 * @description: Job配置
 * @author: leigang
 * @create: 2020-01-01 19:35
 */
@Configuration
@EnableBatchProcessing
public class JobConfiguration {
    //注入创建对象的类
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    //任务执行由STEP决定
    //注入创建STEp对象的对象
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    //创建任务对象
    @Bean
    public Job helloJob(){
        return jobBuilderFactory.get("helloJob").start(step1()).build();
    }

    @Bean
    public Step step1(){
        return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
            @Override
            public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
                System.out.println("hello Job");
                return RepeatStatus.FINISHED;
            }
        }).build();
    }
}
